package cn.chenxiejia;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @version : V1.0
 * @program: maven
 * @ClassName: Stream
 * @Description:
 * @Author: chenxiejia
 * @Date: 2022/1/26 10:48
 */
public class Stream1 {
    public static void main(String[] args) {
        //IntStream.iterate(1, (e) -> e + 2).limit(10).skip(2).forEach(System.out::println);
        IntStream is = IntStream.of(20, 40, 30, 10, 50, 12, 36, 48, 15, 26, 12, 12, 15, 15);
        //distinct()去除重复值
        //is.distinct().forEach(System.out::println);
        //everage()求平均值
//        System.out.println(is.average().getAsDouble());
        //rangeClosed生成范围内的数；
        IntStream.rangeClosed(1, 100).forEach(System.out::println);
        IntStream.range(1, 20).forEach(System.out::print);
        //rangeclosed包括最后一个元素
        System.out.println(IntStream.rangeClosed(1, 100).sum());
        System.out.println();
        //interate迭代  limit限制个数  filter过滤
        IntStream.iterate(1, a -> a + 2).limit(10).filter(a -> a > 5).forEach(System.out::print);
        IntStream is1 = IntStream.of(20, 40, 30, 10, 50, 12, 36, 48, 15, 26, 12, 12, 15, 15);
        //Stream转化为list;boxed装箱,collect收集器 Collectors.tolist转化为list
        List<Integer> list = is1.boxed().collect(Collectors.toList());
        System.out.println(list);
        //List转化为Stream
        List<Integer> list1 = new ArrayList<Integer>(List.of(1, 0, 15, 26, 15, 48, 26, 95, 75, 84));
        var is3 = list1.stream().mapToInt(i -> i);
        System.out.println();
        is3.forEach(System.out::print);
        System.out.println();

        var is4 = IntStream.of(1, 5, 26, 14, 25, 31, 24, 14, 11, 10);
        //升序
        //is4.sorted().forEach(System.out::println);

        //降序要先装箱再排序
        //is4.boxed().sorted((a, b) -> b - a).forEach(System.out::println);
        //乱序先装箱再排序
        //Random rand = new Random();
        //is4.boxed().sorted((a, b) -> rand.nextBoolean() ? 1 : -1).forEach(System.out::println);
//        int[] a = new int[]{1, 5, 4, 8, 6, 48, 15, 48, 26};
//        System.out.println(Arrays.stream(a).filter(v -> v > 10).sum());
        //按行读取转化成流
//        String str = "java\njavascript\nmysql\nhtml5\njava8\njava17";
//        var sss = str.lines();
//        sss.filter(a -> a.contains("java")).map(a -> String.format("Hello:%s", a)).forEach(System.out::print);
        //字符串流分割
        Pattern pattern = Pattern.compile("\\s?");
        String s = "hello,world,java,javaweb,c";
        pattern.splitAsStream(s).forEach(System.out::println);


    }
}
